package avicit.bdp.dlm.service;


import avicit.bdp.common.base.BaseService;
import avicit.bdp.common.dto.NodeStorageDetail;
import avicit.bdp.core.file.util.FileUtils;
import avicit.bdp.dlm.dao.DlmHiveStatDao;
import avicit.bdp.dlm.dto.DlmHiveStatDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * hive统计服务
 *
 * @author xugb
 * @date 2020/12/1 9:22
 */
@Service
public class DlmHiveStatService extends BaseService<DlmHiveStatDao, DlmHiveStatDTO> {


    public List<NodeStorageDetail> getTop5TableAmountList(String dataSourceId,String statDate) {
        List<NodeStorageDetail> nodeStorageDetails = new ArrayList<>();
        List<NodeStorageDetail> list = new ArrayList<>();
        DlmHiveStatDTO dlmHiveStatDTO = new DlmHiveStatDTO();
        dlmHiveStatDTO.setDataSourceId(dataSourceId);
        dlmHiveStatDTO.setStatDate(statDate);
        List<DlmHiveStatDTO> dtoList = selectList(dlmHiveStatDTO);
        if(CollectionUtils.isNotEmpty(dtoList)){
            for (DlmHiveStatDTO hiveStatDTO : dtoList) {
                NodeStorageDetail nodeStorageDetail = new NodeStorageDetail();
                nodeStorageDetail.setName(hiveStatDTO.getTableName());
                nodeStorageDetail.setAmount(hiveStatDTO.getTableAmount());
                String unit = FileUtils.formatNumber(hiveStatDTO.getTableAmount());
                nodeStorageDetail.setStorage(FileUtils.formatAmountUnit(unit, hiveStatDTO.getTableAmount()));
                nodeStorageDetail.setStorageUnit(unit);
                nodeStorageDetails.add(nodeStorageDetail);
            }
            list = nodeStorageDetails.stream()
                    .sorted(Comparator.comparing(NodeStorageDetail::getAmount).reversed()).limit(5)
                    .collect(Collectors.toList());
        }
        return list;
    }
}
